package com.shell.governor.modules.controller;

import java.io.InputStream;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.shell.bee.base.annotation.Logging;
import com.shell.bee.base.utils.StringUtil;
import com.shell.bee.entity.mvc.RetMsg;
import com.shell.bee.file.utils.CommonFileUtil;
import com.shell.bee.mvc.controller.BaseController;
import com.shell.bee.persistence.model.PageLimit;
import com.shell.bee.report.excel.WordParser;
import com.shell.framework.utils.CommonDataUtil;
import com.shell.governor.modules.po.PubDictionary;
import com.shell.governor.modules.po.PubProtocol;
import com.shell.governor.modules.service.PubDictionaryService;
import com.shell.governor.modules.service.PubProtocolService;

@RestController
public class PubProtocolController extends BaseController {
	@Autowired
	private PubProtocolService pubProtocolService;
	@Autowired
	private PubDictionaryService pubDictionaryService;

	@RequestMapping
	@Logging(module = "��������", methods = "����������������")
	public List<PubProtocol> getProtocolListByPage(PageLimit limit, PubProtocol pubProtocol, boolean finFlag) {
		pubProtocol.setCreatorId(CommonDataUtil.getCurEntId());
		return this.pubProtocolService.selectProtocolListByPage(limit, pubProtocol, finFlag);
	}

	@RequestMapping
	@Logging(module = "��������", methods = "��������")
	public Map<String, Object> uploadProtocol(@RequestParam(value = "file", required = false) MultipartFile file,
			String fileType, String savePath) throws Exception {
		return this.pubProtocolService.uploadProtocol(file, fileType, savePath);
	}

	@RequestMapping
	@Logging(module = "��������", methods = "������������������")
	public RetMsg saveOrUploadProtocol(PubProtocol protocol, String fileIds, String finIds) {
		String str = "����";
		if (StringUtil.emptyAndNull(protocol.getProtoId())) {
			str = "����";
		}
		int count = this.pubProtocolService.saveOrdUploadProtocol(protocol, fileIds, finIds);
		if (count > 0) {
			return initRetMsg("success", str + "����");
		}
		return initRetMsg("error", str + "����");
	}

	@RequestMapping
	@Logging(module = "��������", methods = "������������")
	public PubProtocol getProtocolDetail(String protoId, boolean convertFlag) {
		if (convertFlag) {
			return convertProtocolContent((PubProtocol) this.pubProtocolService.selectByKey(protoId));
		}
		return (PubProtocol) this.pubProtocolService.selectByKey(protoId);
	}

	@RequestMapping
	@Logging(module = "协议管理", methods = "获取具体协议")
	public PubProtocol getProtocolByGpAndType(String finId, String groupId, String protoType) {
		return this.pubProtocolService.selectProtocolByGpAndType(finId, groupId, protoType);
	}

	@RequestMapping
	@Logging(module = "��������", methods = "����������������������")
	public String getProtoInvalidKeys(String finId, String grpId, String excuType) {
		return this.pubProtocolService.selectProtoInvalidKeys(finId, grpId, excuType);
	}

	@RequestMapping
	@Logging(module = "��������", methods = "������������������������������")
	public String checkFinProto(String protoId, String finIds, String grpId) {
		return this.pubProtocolService.checkFinProto(protoId, finIds, grpId);
	}

	@RequestMapping
	@Logging(module = "��������", methods = "��������")
	public RetMsg delProtocol(String protoId) {
		this.pubProtocolService.delProtocolById(protoId);
		return initRetMsg("success", "����������");
	}

	@RequestMapping
	@Logging(module = "��������", methods = "��������������")
	public List<String> getFinIds(String protoId, String protoType) {
		return this.pubProtocolService.selectFinIds(protoId, protoType);
	}

	@RequestMapping
	@Logging(module = "��������", methods = "������������")
	public PubProtocol getAuthProtocol() {
		return convertProtocolContent(this.pubProtocolService.selectAuthProtocol());
	}

	@RequestMapping
	@Logging(module = "��������", methods = "��������")
	public void downloadProto(String protoId, HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		Map<String, Object> map = this.pubProtocolService.selectProtocolForDownload(protoId);
		if (map.size() > 0) {
			String fileNmae = (String) map.keySet().iterator().next();
			InputStream is = WordParser.htmlToWord(fileNmae, map.get(fileNmae).toString());
			CommonFileUtil.writeFileToClient(fileNmae, is, response, request);
		}
	}

	private PubProtocol convertProtocolContent(PubProtocol pubProtocol) {
		if (pubProtocol != null) {
			String protoType = pubProtocol.getProtoType();
			String content = pubProtocol.getContent();
			List<PubDictionary> dicts = this.pubDictionaryService.selectChildDictByParentCode(protoType);
			for (PubDictionary dct : dicts) {
				String replaceText = dct.getExtension1();
				content = content.replace(replaceText, "");
			}
			pubProtocol.setContent(content);
		}
		return pubProtocol;
	}
}
